// pages/tools/yunqian/zwjl.js
var _$ = require("../../../common/wxjs/wx_ajax");
const url = "yq_zwjl";
var errorMessage = {
  "heightIsEmpty": "请输入您的身高!",
  "weightIsEmpty": "请输入您的孕前体重！",
  "heightIsInvalid": "身高请输入阿拉伯数字",
  "weightIsInvalid": "孕前体重请输入阿拉伯数字",
  "heightIsNotInScope": "您的身高不在范围内",
  "weightIsNotInScope": "您的体重不在范围内"
};
var suggestMessage = ["您要注意控制体重了！", "偏瘦", "正常", "略胖", "太胖"];
var heightScope = { min: 100, max: 300 }; //单位厘米
var weightScope = { min: 30, max: 200 }; //单位公斤
Page({
  /**
   * 页面的初始数据
   */
  data: {
    uesrtoken: null,
    formdata: {
      "m_age": '',
      "m_jhage": '',
      "m_height": '',
      "m_nation": '',
      "m_BMI": '',
      "m_account": '',
      "m_health": 0,
      "f_age": '',
      "f_jhage": '',
      "f_height": '',
      "f_nation": '',
      "f_BMI": '',
      "f_account": '',
      "f_health": 0,
      "byinput": '',
    }
  },
  // 提交表单
  formSubmit: function (e) {
    var that = this;
    var userdata = e.detail.value;
    userdata = JSON.stringify(userdata);
    var litToken = that.data.uesrtoken;
    var type = 1;
    var methods = "POST";
    var header = "application/x-www-form-urlencoded";
    var dataobj = { type, userdata, litToken };
    wx.showToast({
      title: '正在提交',
      icon: 'loading',
      duration: 2000
    })
    _$.sendRrquest(url, methods, dataobj, header)
      .then(function (response) {
        console.log(response)
        if (response.data.status == 200) {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          });
          setTimeout(function () {
            wx.hideToast()
          }, 2000)
        } else {
          wx.showToast({
            title: '数据未修改',
            icon: 'loading',
            duration: 2000
          });
          setTimeout(function () {
            wx.hideToast()
          }, 2000)
        }
      }, function (error) {
        wx.showToast({
          title: '提交失败',
          duration: 2000
        });
        setTimeout(function () {
          wx.hideToast()
        }, 2000)
        console.log(error);
      });
  },
  // 请求数据
  getdata: function (val) {
    var that = this;
    var type = 0;
    var litToken = val;
    var dataobj = { type, litToken };
    var methods = "GET";
    var header = "application/json";
    wx.showLoading({
      title: '加载中',
    })
    _$.sendRrquest(url, methods, dataobj, header)
      .then(function (response) {
        setTimeout(function () {
          wx.hideLoading()
        }, 1000)
        console.log(response)
        that.setData({
          formdata: response.data.data.res
        });
      }, function (error) {
        setTimeout(function () {
          wx.hideLoading()
        }, 2000)
        wx.showToast({
          title: '网络错误，请稍后再试',
          duration: 2000
        })
        setTimeout(function () {
          wx.hideToast()
        }, 2000)
        console.log(error);
      });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getStorage({
      key: 'token',
      success: function (res) {
        that.setData({
          uesrtoken: res.data
        })
        that.getdata(res.data)
      }
    })
  },
  //验证字段有效性 
  checkValid: function (obj) {
    if (!obj.value) {
      obj.focus();
      return { suc: 0, info: errorMessage[obj.name + "IsEmpty"] };
    }
    if (isNaN(obj.value)) {
      obj.value = "";
      obj.focus();
      return { suc: 0, info: errorMessage[obj.name + "IsInvalid"] };
    }
    if ((obj.name === "height") && (obj.value >= heightScope.max || obj.value < heightScope.min)) {
      obj.value = "";
      obj.focus();
      return { suc: 0, info: errorMessage[obj.name + "IsNotInScope"] };
    }
    if ((obj.name === "weight") && (obj.value > weightScope.max || obj.value < weightScope.min)) {
      obj.value = "";
      obj.focus();
      return { suc: 0, info: errorMessage[obj.name + "IsNotInScope"] };
    }
    return { suc: 1, info: "正确" };
  },
  //计算各个指标
  calculatePower: function (yheight, yweight) {
    console.log(yheight, yweight)
    var biaozhong = ((yheight * yheight) * 21 / 10000).toFixed(2); //孕前标准体重
    var feidu = ((yweight - biaozhong) / biaozhong * 100).toFixed(2); //孕前肥胖度
    var BMI = ((yweight / (yheight * yheight)) * 10000).toFixed(2); //肥胖度BMI值
    var lizhong = ((BMI * 0.88 + 6.65) * (yheight * yheight) / 10000).toFixed(2); //安产理想体重 
    return { biaozhong: biaozhong, feidu: feidu, BMI: BMI, lizhong: lizhong };
  },
  warnText:function (val) {
    var warnstr;
    console.log(val)
    if(val < 20) {
      warnstr = "偏瘦";
    }
        if (val >= 20 && val < 24) {
      warnstr = "正常";
    }
        if (val >= 24 && val < 26.4) {
      warnstr = "略胖";
    }
        if (val >= 26.4) {
      warnstr = "太胖";
    }
    return warnstr
  },
  mHeight:function(e){
    var that = this;
    that.setData({
      "formdata.m_height": e.detail.value
    })
    var motherData = that.calculatePower(e.detail.value, that.data.formdata.m_nation);
    var warnstr = that.warnText(motherData.BMI)
    that.setData({
      "formdata.m_BMI": motherData.BMI,
      "formdata.m_account": warnstr
    })
  },
  mWeight: function (e) {
    var that = this;
    this.setData({
      "formdata.m_nation": e.detail.value
    })
    var motherData = that.calculatePower(that.data.formdata.m_height, e.detail.value);
    var warnstr = that.warnText(motherData.BMI);
    that.setData({
      "formdata.m_BMI": motherData.BMI,
      "formdata.m_account": warnstr
    })
  },
  fHeight: function (e) {
    var that = this;
    that.setData({
      "formdata.f_height": e.detail.value
    })
    var fatherData = that.calculatePower(e.detail.value, that.data.formdata.f_nation);
    var warnstr = that.warnText(fatherData.BMI)

    that.setData({
      "formdata.f_BMI": fatherData.BMI,
      "formdata.f_account": warnstr
    })
  },
  fWeight: function (e) {
    var that = this;
    this.setData({
      "formdata.f_nation": e.detail.value
    })
    var fatherData = that.calculatePower(that.data.formdata.f_height, e.detail.value);
    var warnstr = that.warnText(fatherData.BMI)
    that.setData({
      "formdata.f_BMI": fatherData.BMI,
      "formdata.f_account": warnstr
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 按钮切换
  btnselect: function (e) {
    var that = this;
    that.setData({
      "formdata.m_health": e.target.dataset.m_health,
      "formdata.f_health": e.target.dataset.f_health,
    })
  },
})